home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet internetowy / Czytniki RSS / RSS Bandit 1.3.0.42 / RssBandit Installer.msi / _172FF5180BC61D3C6D240F14338A23AA / _1F37BFA4F398478B820308CCE7FDF391 < prev    next >
Text File  |  2005-03-12  |  6KB  |  215 lines

  1. using System;
  2. using System.IO;
  3. using System.Xml.XPath;
  4. using Syndication.Extensibility;
  5. using System.Windows.Forms;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using Microsoft.Win32;
  9. using System.Xml.Xsl;
  10. using System.Xml;
  11. using System.Xml.Serialization;
  12. using System.Security.Cryptography;
  13. using System.Net; 
  14.  
  15. namespace BlogExtension.Delicious 
  16. {
  17.     public class DeliciousPlugin : IBlogExtension
  18.     {        
  19.         string configFile;    
  20.         delicious configInfo; 
  21.         TripleDESCryptoServiceProvider cryptoProvider; 
  22.         XmlSerializer serializer; 
  23.  
  24.         public DeliciousPlugin()
  25.         {
  26.             //setup path to config file
  27.             string assemblyUri = this.GetType().Assembly.CodeBase;
  28.             string assemblyPath = new Uri(assemblyUri).LocalPath;
  29.             string assemblyDir = Path.GetDirectoryName(assemblyPath);
  30.             configFile = Path.Combine(assemblyDir, "delicious.xml");    
  31.         
  32.             //set up crypto provider for encrypting password in config file
  33.             cryptoProvider = new TripleDESCryptoServiceProvider();
  34.             cryptoProvider.Key = CalculateHash();
  35.             cryptoProvider.Mode = CipherMode.ECB;
  36.  
  37.             //setup XML serializer for config file
  38.             serializer = new XmlSerializer(typeof(delicious));        
  39.         }
  40.  
  41.         public string DisplayName { get { return Resource.Manager["RES_MenuDeliciousCaption"]; } }
  42.  
  43.         public bool HasConfiguration { get { return true; } }
  44.  
  45.         public bool HasEditingGUI { get { return true; } }
  46.  
  47.         public void Configure(IWin32Window parent)
  48.         {
  49.             this.LoadConfig(); 
  50.  
  51.             using (DeliciousPluginConfigurationForm configForm = new DeliciousPluginConfigurationForm(configInfo.username, this.Decrypt(configInfo.password), configInfo.apiurl))
  52.             {
  53.                 if (configForm.ShowDialog(parent) == DialogResult.OK)
  54.                 {                    
  55.                     configInfo.apiurl        = configForm.textUri.Text;
  56.                     configInfo.username      = configForm.textUser.Text;
  57.                     configInfo.password      = this.Encrypt(configForm.textPwd.Text);
  58.  
  59.                     XmlTextWriter writer     = new XmlTextWriter(configFile, Encoding.UTF8); 
  60.                     serializer.Serialize(writer, configInfo); 
  61.                 }
  62.             }
  63.         }
  64.  
  65.  
  66.         public void BlogItem(IXPathNavigable rssFragment, bool edited) {
  67.  
  68.             HttpWebRequest request = null;
  69.             HttpWebResponse response = null;
  70.  
  71.             try{ 
  72.  
  73.                 this.LoadConfig();                 
  74.  
  75.                 string tags, description = rssFragment.CreateNavigator().Evaluate("string(//item/title/text())").ToString(), 
  76.                   url = rssFragment.CreateNavigator().Evaluate("string(//item/link/text())").ToString();
  77.  
  78.  
  79.                 using (DeliciousPostForm postForm = new DeliciousPostForm(url, description)) {
  80.                     if (postForm.ShowDialog() == DialogResult.OK) {                    
  81.                         tags = postForm.textTags.Text; 
  82.                         url = postForm.textUri.Text;
  83.                         description = postForm.textDescription.Text;
  84.                     
  85.                         Uri postUrl = new Uri(configInfo.apiurl + "?url=" + url + "&tags=" + tags + "&description=" + description); 
  86.                         request = (HttpWebRequest) WebRequest.Create(postUrl);
  87.                         request.UserAgent            = "del.icio.usIBlogExtensionPlugin/1.0"; 
  88.                         
  89.                         NetworkCredential nc = new NetworkCredential(configInfo.username, this.Decrypt(configInfo.password));                         
  90.                         request.Credentials = nc; 
  91.     
  92.                         response = (HttpWebResponse) request.GetResponse(); 
  93.                         
  94.                         if(response.StatusCode != HttpStatusCode.OK){
  95.                             throw new Exception(new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd()); 
  96.                         }
  97.                         
  98.                     }//if (postForm.ShowDialog() == DialogResult.OK) {    
  99.                 }//using 
  100.  
  101.             }catch(Exception e){
  102.                 MessageBox.Show(e.Message, e.GetType().Name,MessageBoxButtons.OK, MessageBoxIcon.Error);
  103.             }finally{
  104.             
  105.                 if(response != null){ 
  106.                     response.Close(); 
  107.                 }
  108.             }
  109.             
  110.         }
  111.  
  112.         private void LoadConfig(){
  113.             
  114.             if(configInfo == null){
  115.  
  116.                 if(File.Exists(configFile)){
  117.  
  118.                     XmlTextReader reader = new XmlTextReader(configFile);
  119.                     configInfo = (delicious) serializer.Deserialize(reader);                     
  120.                     reader.Close();
  121.             
  122.                 }else{
  123.                     configInfo = new delicious(); 
  124.                     configInfo.apiurl = "http://del.icio.us/api/posts/add"; 
  125.                 }
  126.             }
  127.         }
  128.         
  129.         private static byte[] CalculateHash() {
  130.             string salt = "DeliciousPlugin.4711";
  131.             byte[] b = Encoding.Unicode.GetBytes(salt);
  132.             int bLen = b.GetLength(0);
  133.                 
  134.             // just to make the key somewhat "invisible" in Anakrino, we use the random class.
  135.             // the seed (a prime number) makes it repro
  136.             Random r = new Random(1500450271);    
  137.             // result array
  138.             byte[] res = new Byte[500];
  139.             int i = 0;
  140.                 
  141.             for (i = 0; i < bLen && i < 500; i++)
  142.                 res[i] = (byte)(b[i] ^ r.Next(30, 127));
  143.                 
  144.             // padding:
  145.             while (i < 500) {
  146.                 res[i] = (byte)r.Next(30, 127);
  147.                 i++;
  148.             }
  149.  
  150.             MD5CryptoServiceProvider csp = new MD5CryptoServiceProvider();
  151.             return csp.ComputeHash(res);
  152.         }
  153.     
  154.         private byte[] Encrypt(string str) {
  155.             byte[] inBytes;
  156.             byte[] ret;
  157.  
  158.             if (str == null)
  159.                 ret = null;
  160.             else {
  161.                 if (str.Length == 0)
  162.                     ret = null;
  163.                 else {
  164.                     try {
  165.                         inBytes = Encoding.Unicode.GetBytes(str);
  166.                         ret = cryptoProvider.CreateEncryptor().TransformFinalBlock(inBytes, 0, (int) inBytes.GetLength(0));
  167.                     }
  168.                     catch (Exception e) {
  169.                         MessageBox.Show("Exception in Encrypt: "+e.ToString(), "CryptHelper");
  170.                         ret = null;
  171.                     }
  172.                 }
  173.             }
  174.             return ret;
  175.         }
  176.  
  177.         private string Decrypt(byte[] bytes) {
  178.             byte[] tmp;
  179.             string ret;
  180.  
  181.             if ((bytes == null) || (bytes.GetLength(0) == 0))
  182.                 ret = String.Empty;
  183.             else {
  184.                 try {
  185.                     tmp = cryptoProvider.CreateDecryptor().TransformFinalBlock(bytes, 0, (int) bytes.GetLength(0));
  186.                     ret = Encoding.Unicode.GetString(tmp);
  187.                 }
  188.                 catch (Exception e) {
  189.                     MessageBox.Show("Exception in Decrypt: "+e.ToString(), "CryptHelper");
  190.                     ret = String.Empty;
  191.                 }
  192.             }
  193.             return ret;
  194.         }            
  195.     
  196.     }
  197.  
  198.  
  199.     /// <remarks/>
  200.     [System.Xml.Serialization.XmlRootAttribute("del.icio.us", Namespace="", IsNullable=false)]
  201.     public class delicious {
  202.     
  203.         /// <remarks/>
  204.         [System.Xml.Serialization.XmlElementAttribute("api-url")]
  205.         public string apiurl;
  206.     
  207.         /// <remarks/>
  208.         public string username;
  209.     
  210.         /// <remarks/>
  211.         public byte[] password;
  212.     }
  213.  
  214. }
  215.